
//获取应用实例
const app = getApp()

Component({
    data: {
        showTopTips: false,
        idCard: null,
        mobile: null,
        formData: {

        },
        rules: [{
            name: 'mobile',
            rules: [{ required: true, message: '请输入手机号'}, {mobile: true, message: '手机号格式不对'}],
        }
        /**,{
            name: 'vcode',
            rules: { required: true, message: '验证码必填' },
          },**/]
    },
    methods: {
      goBack: function(){
        wx.navigateBack({
          delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
        });
      },
        formInputChange(e) {
          const { field } = e.currentTarget.dataset
          this.setData({
            [`formData.${field}`]: e.detail.value
          })
        },
        submitForm() {
            this.selectComponent('#form').validate((valid, errors) => {
                console.log('valid', valid, errors)
                if (!valid) {
                    const firstError = Object.keys(errors)
                    if (firstError.length) {
                        this.setData({
                            error: errors[firstError[0]].message
                        })
    
                    }
                } else {
                    // 登录
                    wx.login({
                      success: res => {
                        // 发送 res.code 到后台换取 openId, sessionKey, unionId
                        console.log(res);
                        wx.request({
                          url: 'https://www.xinhuo.site/api/login',
                          data: {
                            code: res.code,
                            mobile: this.data.formData.mobile
                          },
                          method: 'POST',
                          header: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                          },
                          success(res) {
                            if (res.data.syscode == 0) {
                              app.globalData.userId = res.data.userId;
                              app.globalData.memberName = res.data.memberName;
                              app.globalData.partyOrgName = res.data.partyOrgName;
                              wx.showToast({
                                title: '校验通过'
                              });
                              wx.navigateBack({
                                delta: 1
                              });
                            }
                            else if (res.data.syscode == 1) {
                              wx.showToast({
                                title: '没有该用户信息',
                                icon: 'none'
                              });
                            }
                            else if (res.data.syscode == 2) {
                              wx.showToast({
                                title: '微信用户与系统用户不匹配',
                                icon: 'none'
                              });
                            }
                            else {
                              wx.showToast({
                                title: '登录失败请联系管理员',
                                icon: 'none'
                              });
                            }
                          }
                        })
                      }
                    });
                }
            })
        }

    }
});